package com.jvtd.eyl.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.constants.App;
import com.jvtd.eyl.utils.ReadUtils;
import com.jvtd.eyl.widget.adapter.BookAdapter;
import com.jvtd.utils.UiUtils;
import com.jvtd.widget.recyclerView.JvtdRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 书籍列表
 *
 * 作者:chenlei
 * 时间:2018/11/14 9:50 AM
 */
public class BookRecyclerView extends JvtdRecyclerView<BookBean> {
    private OnSelectedChangeListener mOnSelectedChangeListener;

    private boolean isEditor = false;//是否编辑模式
    private BookAdapter mAdapter;

    public BookRecyclerView(@NonNull Context context) {
        super(context);
    }

    public BookRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BookRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public interface OnAddListener {
        void onAddBook(BookBean bookBean);
    }

    private OnAddListener onAddListener;

    public void setOnAddListener(OnAddListener onAddListener) {
        this.onAddListener = onAddListener;
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        mAdapter = new BookAdapter();
//        mAdapter.openLoadAnimation();
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            BookBean bookBean = mAdapter.getItem(position);
            if (bookBean != null) {
                if (!isEditor) {
                    switch (view.getId()) {
                        case R.id.show_btn:
                            ReadUtils.readFile(mContext,mAdapter.getData(),position);
                            break;
                        case R.id.add_btn:
                            if (onAddListener != null)
                                onAddListener.onAddBook(bookBean);
                            break;
                    }
                } else {
                    if (mOnSelectedChangeListener != null)
                        mOnSelectedChangeListener.onSelectChange();
                }
            }
        });
        DividerItemDecoration divider = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.shape_magic_indicator_line);
        if (drawable != null) {
            divider.setDrawable(drawable);
        }
        addItemDecoration(divider);
        return mAdapter;
    }

    @Override
    public LayoutManager initLayoutManager() {
        return new GridLayoutManager(mContext, 3);
    }

    @Override
    public boolean isFirst() {
        return true;
    }

    @Override
    public int getLoadNum() {
        return 0;
    }

    /**
     * 是否开启编辑模式
     * @param editor 是与否
     */
    public void editor(boolean editor) {
        isEditor = editor;
        if (!editor) {
            for (BookBean myBookBean : mAdapter.getData()) {
                myBookBean.setSelected(false);
            }
        }
        mAdapter.setSelected(editor);
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 是否选择全部
     * @param all 是与否
     */
    public void selectAll(boolean all) {
        if (isEditor) {
            for (BookBean myBookBean : mAdapter.getData()) {
                if (!TextUtils.equals(BookBean.YES, myBookBean.getIsAdd()))
                    myBookBean.setSelected(all);
            }
        }
        if (mOnSelectedChangeListener != null)
            mOnSelectedChangeListener.onSelectChange();
    }

    public void setOnSelectedChangeListener(OnSelectedChangeListener onSelectedChangeListener) {
        mOnSelectedChangeListener = onSelectedChangeListener;
    }

    //是否编辑模式
    public boolean isEditor() {
        return isEditor;
    }

    //获取选择的书籍信息
    public List<BookBean> getSelectedData() {
        List<BookBean> myBookBeans = new ArrayList<>();
        for (BookBean myBookBean : mAdapter.getData()) {
            if (myBookBean.isSelected())
                myBookBeans.add(myBookBean);
        }
        return myBookBeans;
    }

    @Override
    public double getFlingRatio()
    {
        return App.FLING_RATIO;
    }
}
